// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   MenuBarQuran.java

package canvas;

import java.util.Vector;
import javax.microedition.lcdui.Graphics;

// Referenced classes of package mquran:
//            MenuItem, QuranCanvas, Quran

public class MenuBarQuran
{

    public MenuBarQuran(Quran midlet, Vector submenu, int flag)
    {
        this.midlet = midlet;
        menus = submenu;
        this.flag = flag;
        width = 0;
        height = 0;
        int hf = QuranCanvas.font.getHeight() + 4;
        for(int i = 0; i < submenu.size(); i++)
        {
            height += hf;
            MenuItem item = (MenuItem)submenu.elementAt(i);
            int w = QuranCanvas.font.stringWidth(item.caption) + WIDHT_SHORTCUT;
            if(w > width)
                width = w;
        }

        width += 5;
        shown = false;
    }

    public void setMenu(int index, MenuItem menu)
    {
        if(index < menus.size())
            menus.setElementAt(menu, index);
    }

    public void draw(Graphics g, int w, int h)
    {
        g.setColor(0xf1cf1b);
        int y = h - height - 3;
        int x;
        if((flag & 2) != 0)
            x = w - width - 6;
        else
            x = 3;
        g.setClip(x - 2, y - 2, width + 4, height + 4);
        g.fillRect(x - 1, y - 1, width + 2, height + 1);
        g.setColor(0xffffff);
        g.drawLine(x - 2, y - 2, x + width + 1, y - 2);
        g.drawLine(x - 2, y - 2, x - 2, y + height);
        g.setColor(0x808080);
        g.drawLine(x - 2, y + height, x + width + 1, y + height);
        g.drawLine(x + width + 1, y - 2, x + width + 1, y + height);
        int hf = QuranCanvas.font.getHeight() + 4;
        g.setFont(QuranCanvas.font);
        for(int i = 0; i < menus.size();)
        {
            if(i == selected)
            {
                g.setColor(0xffe25a);
                g.fillRect(x, y, width, hf);
            }
            g.setColor(0);
            MenuItem item = (MenuItem)menus.elementAt(i);
            g.drawString(item.caption, x + 2, y + 2, 20);
            if(item.shortcat > 0)
            {
                String s = " [" + (char)item.shortcat + "]";
                g.drawString(s, width, y + 2, 24);
            }
            if(i > 0)
            {
                g.setColor(0x808080);
                g.drawLine(x, y - 1, width + 2, y - 1);
                g.setColor(0xffffff);
                g.drawLine(x, y, width + 2, y);
            }
            i++;
            y += hf;
        }

    }

    public boolean keyPressed(int keycode)
    {
        if(keycode == Quran.KEYDOWN)
        {
            if(selected < menus.size() - 1)
                selected++;
            else
                selected = 0;
            return true;
        }
        if(keycode == -1)
        {
            if(selected > 0)
                selected--;
            else
                selected = menus.size() - 1;
            return true;
        }
        if(keycode == -5 || keycode == -6)
        {
            midlet.onMenu(this, selected, ((MenuItem)menus.elementAt(selected)).caption);
            shown = false;
            return true;
        }
        if(keycode == -7)
        {
            shown = false;
            return true;
        }
        if(shortcutPress(keycode))
        {
            shown = false;
            return true;
        } else
        {
            return false;
        }
    }

    public boolean shortcutPress(int key)
    {
        for(int i = menus.size() - 1; i >= 0; i--)
        {
            MenuItem item = (MenuItem)menus.elementAt(i);
            if(item.shortcat == key)
            {
                midlet.onMenu(this, i, item.caption);
                return true;
            }
        }

        return false;
    }

    public void show()
    {
        shown = true;
        selected = 0;
    }

    public static final int MENU_LEFT = 1;
    public static final int MENU_RIGHT = 2;
    private Vector menus;
    int width;
    int height;
    private int selected;
    private Quran midlet;
    boolean shown;
    private int flag;
    private static int WIDHT_SHORTCUT;

    static 
    {
        WIDHT_SHORTCUT = QuranCanvas.font.stringWidth(" [#]");
    }
}
